package com.iprojectmanager.businessclass;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.iprojectmanager.managers.*;
import com.iprojectmanager.entities.*;

/**
 * @author Jons
 */
public class RolControl extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        if(request.getParameter("operacionesroles") != null)
        {
                RequestDispatcher disp = request.getRequestDispatcher("VistaOperacionRol");
                disp.forward(request, response);

        }else if(request.getParameter("operacion") != null)
        {
                if(request.getParameter("operacion").compareTo("Eliminar") == 0)
                {
                    RolManager manager = new RolManager();
                    UserManager usermanager = new UserManager();
                    Rol rol = manager.getRoles(Integer.valueOf(request.getParameter("rol")));
                        for(IntegranteProyecto integrante : rol.getIntegrantes())
                        {
                            integrante.removeRol(rol);
                            usermanager.exec(integrante,IProjectManager.UPDATE);
                        }
                    manager.exec(rol,RolManager.DELETE);
                    RequestDispatcher disp = request.getRequestDispatcher("VistaAdministrador");
                    disp.forward(request, response);
                }
                else
                {
                    RequestDispatcher disp = request.getRequestDispatcher("VistaOperacionRol");
                    request.setAttribute("tipo",request.getParameter("operacion"));
                    disp.forward(request, response);
                }
        }
        else if(request.getParameter("CA") != null)
        {
            saveRol(request);
            RequestDispatcher disp = request.getRequestDispatcher("VistaAdministrador");
            disp.forward(request, response);
        }
        else if(request.getParameter("MA") != null)
        {
            changeRol(request);
            RequestDispatcher disp = request.getRequestDispatcher("VistaAdministrador");
            disp.forward(request, response);
        }
    }

    public void saveRol(HttpServletRequest request)
    {
        String nombre = request.getParameter("tbNombreRol");
        RolManager rolmanager = new RolManager();

            if(nombre.compareTo("") != 0 && rolmanager.getRoles(nombre) == null)
            {
                Rol rol = new Rol(nombre);
                IProjectManager manager = new IProjectManager();
                manager.exec(rol, IProjectManager.INSERT);
            }
    }

    public void changeRol(HttpServletRequest request)
    {
        String id = request.getParameter("tbIdRol");
        String nombre = request.getParameter("tbNombreRol");

            if(nombre.compareTo("") != 0)
            {
                RolManager manager = new RolManager();
                Rol rol = manager.getRoles(Integer.valueOf(id));
                rol.setNombre(nombre);
                manager.exec(rol, IProjectManager.UPDATE);
            }
    }

    @Override
    public String getServletInfo()
    {
        return "Manejo de ABC de roles";
    }

}